package com.dss.auto_deploy.controller;

import com.dss.auto_deploy.dto.AllDockerInfoDto;
import com.dss.auto_deploy.dto.ChangeTagDto;
import com.dss.auto_deploy.dto.OperationDto;
import com.dss.auto_deploy.service.IAdmDockerDeployService;
import com.dss.auto_deploy.service.IAdmRegistryTagHstService;
import com.dss.auto_deploy.util.Result;
import com.dss.auto_deploy.util.ServiceException;
import com.dss.auto_deploy.vo.AllDockerInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @Description:
 * @author: dongshoushan
 * @date: 2022年10月10日 10:53
 */
@RestController
@RequestMapping("/totalOverview")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags ="99: 总览")
public class DockerTotalOverviewController {
    private final IAdmDockerDeployService admDockerDeployService;
    private final IAdmRegistryTagHstService admRegistryTagHstService;

    @PostMapping("/getAllDockerInfo")
    @ApiOperation(value = "查询docker部署详情", notes="查询docker部署详情")
    public Result<List<AllDockerInfoVo>> getAllDockerInfo(@RequestBody @Valid AllDockerInfoDto dto) throws InterruptedException {
        List<AllDockerInfoVo> list = admDockerDeployService.getAllDockerInfo(dto);
        return Result.success(list);
    }
    @PostMapping("/operation")
    @ApiOperation(value = "批量操作同一个项目模块", notes="批量操作同一个项目模块")
    public Result operation(@RequestBody @Valid OperationDto dto) {
        try {
            admDockerDeployService.operation(dto);
        }catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
        return Result.success();
    }

    @PostMapping("/changeTag")
    @ApiOperation(value = "版本批量切换", notes="版本批量切换")
    public Result changeTag(@RequestBody @Valid ChangeTagDto dto) {
        try {
            admDockerDeployService.changeTag(dto);
        }catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
        return Result.success();
    }
    @ApiOperation(value = "导入jar包镜像", notes="导入jar包镜像")
    @PostMapping(value = "importJarImages", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result importJarImages(@RequestParam(value = "projectId") @ApiParam(value = "项目id") Long projectId,
                                  @RequestParam(value = "namespaceId") @ApiParam(value = "命名空间id") Long namespaceId,
                                  @RequestPart(value = "file") MultipartFile file, HttpServletRequest request) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        admRegistryTagHstService.importJarImages(projectId,namespaceId,file);
        return Result.success();
    }
}
